﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;
using System.Windows.Input;

using CustomControlDatabaseInterface.Model;
using CustomControlDatabaseInterface.Utilities;

namespace CustomControlDatabaseInterface.ViewModel
{
    public class CustomerDetailEntryViewModel : ViewModelBase
    {
        public String Technology { get; set; }

        public String ToolTipText {
            get {
                return "Add new " + Technology + " control";
            }
        }

        private bool isAddControlVisible;

        public bool IsAddControlVisible
        {
            get
            {
                return isAddControlVisible;
            }
            set
            {
                isAddControlVisible = value;
                IsAddControlButtonEnabled = !value; 
                OnPropertyChanged("IsAddControlVisible");
            }
        }

        private bool isAddControlButtonEnabled; 

        public bool IsAddControlButtonEnabled {
            get {
                return isAddControlButtonEnabled; 
            }
            set {
                isAddControlButtonEnabled = value;
                OnPropertyChanged("IsAddControlButtonEnabled");
            }
        }

        public ObservableCollection<CustomerDetailControlDetailViewModel> Controls { get; set; }

        public CustomerDetailAddControlViewModel AddControlViewModel { get; set; }

        public ICommand AddControlCommand { get; set; }

        private CustomerTechControls customerTechControls; 


        public CustomerDetailEntryViewModel(String customerName, String technology) {
            Technology = technology;
            IsAddControlButtonEnabled = true; 
            Controls = new ObservableCollection<CustomerDetailControlDetailViewModel>();
            customerTechControls = new CustomerTechControls(customerName, technology);
            UpdateObservableCollection(Controls, ConvertStringControls(customerTechControls.List));

            AddControlViewModel = new CustomerDetailAddControlViewModel();
            AddControlCommand = new RelayCommand(param => OnAddControlCommand());

        }

        public void UpdateCustomerTechControls() {
            customerTechControls.UpdateList(GetStringListFromControls());
        }

        private List<CustomerDetailControlDetailViewModel> ConvertStringControls(List<String> controls ) {
            List<CustomerDetailControlDetailViewModel> resultList = new List<CustomerDetailControlDetailViewModel>();
            foreach (string item in controls) {
                CustomerDetailControlDetailViewModel detailControl = new CustomerDetailControlDetailViewModel(item);
                detailControl.RemoveControl += OnRemoveControl; 
                resultList.Add(detailControl);
            }
            return resultList; 
        }

        private void OnAddControlCommand() {
            AddControlViewModel.InitializeAddControlViewModel(GetStringListFromControls(), Technology);
            AddControlViewModel.AddControl += this.OnControlAdded;
            AddControlViewModel.Cancel += delegate { IsAddControlVisible = false; };
            IsAddControlVisible = true; 
        }

        private void OnControlAdded(String control) {
            Controls.Add(new CustomerDetailControlDetailViewModel(control));
            AddControlViewModel.AddControl -= OnControlAdded;
            IsAddControlVisible = false;
        }

        private void OnRemoveControl( CustomerDetailControlDetailViewModel control ) {
            control.RemoveControl -= OnRemoveControl;
            Controls.Remove(control);
        }

        private List<String> GetStringListFromControls() {
            List<String> result = new List<string>();
            foreach (CustomerDetailControlDetailViewModel item in Controls) {
                result.Add(item.ControlName);
            }
            return result;
        }
 


    }
}
